#!/usr/bin/perl
#save_server.cgi

require './servermin-lib.pl';
&ReadParse();
&error_setup($text{'join_domain_error'});

if (($in{'hostnamein'} =~ /[^A-Za-z0-9\_\-]/)||($in{'hostnamein'} eq ""))
{
	&nas_syslog("servermin", "naslog_warning", 33005, "Share name invalid!");
	&error($text{'hostname_invalid'});	
}
if (length($in{'hostnamein'}) > 14)
{
	&nas_syslog("servermin", "naslog_warning", 33006, "Share name too long!");
	&error($text{'hostname_toolong'});
}

if ($in{'memberrdn'} =~ /0/)
{
	if (($in{'workgroupin'} =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)\.]/)||($in{'workgroupin'} eq ""))
	{
		&nas_syslog("servermin", "naslog_warning", 33007, "workgroup invalid!");
		&error($text{'workgroup_invalid'});	
	}	
}
else
{
	if (($in{'domainin'} =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)\.]/)||($in{'domainin'} eq ""))
	{
		&nas_syslog("servermin", "naslog_warning", 33008, "Domain invalid!");
		&error($text{'domain_invalid'});	
	}
	if (($in{'pdcin'} =~ /[^A-Za-z0-9\x7f-\xff\_\-\(\)\.]/))
	{
		&nas_syslog("servermin", "naslog_warning", 33009, "PDC invalid!");
		&error($text{'pdc_invalid'});	
	}	
}	

&foreign_require("net", "net-lib.pl");

&foreign_call("net", "save_hostname", $in{'hostnamein'});
&lock_file($config{'hostsfile'});
$f = &read_file_lines($config{'hostsfile'});
foreach $line (@$f)
{
	if ($line =~ /127.0.0.1/)
	{
		$line = "127.0.0.1    ".$in{'hostnamein'}." localhost.localdomain localhost"; 
	}
}
&flush_file_lines();
&unlock_file($config{'hostsfile'});
&nas_syslog("servermin", "naslog_info", 33001, "Change host name to $in{'hostnamein'}");

$success = 0;
&foreign_require("samba", "samba-lib.pl");
%econfig = &foreign_config("samba");
&lock_file($econfig{'smb_conf'});
$global = &foreign_call("samba", "get_share", "global");

#&foreign_call("samba", "setval", "netbios name", $in{'hostnamein'});
if ($in{'oldsecurity'} =~ /^ADS/)
{
    system("/usr/bin/net ads leave");
    sleep(3);
    if($in{'memberrdn'} =~ /0/){
    	&foreign_call("samba","setval","security","user", "");
	&foreign_call("samba","delval","idmap gid");
	&foreign_call("samba","delval","idmap uid");
	&foreign_call("samba","delval","winbind uid");
	&foreign_call("samba","delval","winbind gid");
	&foreign_call("samba","delval","realm");
	&foreign_call("samba","delval","netbios name");
	&foreign_call("samba","delval","server signing");
	&foreign_call("samba","delval","winbind enum users");
	&foreign_call("samba","delval","winbind enum groups");
	&foreign_call("samba","delval","null passwords");
	&foreign_call("samba","delval","client use spnego");
	&foreign_call("samba","delval","winbind separator");
    }
}
if ($in{'memberrdn'} =~ /0/)
{
	&foreign_call("samba","setval","workgroup",$in{'workgroupin'}, "");
	#&foreign_call("samba","setval","password server","", "");
	if ($in{'oldsecurity'} =~ /^ADS/ )
	{
		&foreign_call("samba","setval","security","user", "");
	}
	&nas_syslog("servermin", "naslog_info", 33002, "Server join into workgroup $in{'workgroupin'}");
}
else
{
	my $str = "echo $in{'passin'}|kinit $in{'userin'}\@$in{'pdcin'}";
	open(FD, ">/sysvol/etc/kinit");
	print FD $str;
	close(FD);
	`chmod a+x /sysvol/etc/kinit`;
        &krbconfig($in{'domainin'}, $in{'pdcin'}, $in{'ipin'});
	#system($str);
	my $out1 = &ez_system("/etc/init.d/winbind", "restart");
	sleep(5);
#	&foreign_call("samba","setval","password server",$in{'pdcin'}, "");
        &foreign_call("samba","setval","realm",$in{'pdcin'}, "");
        &foreign_call("samba","setval","workgroup",$in{'domainin'}, "");
	&foreign_call("samba","setval","netbios name",$in{'hostnamein'}, "");
        &foreign_call("samba","setval","security","ADS", "");
	&foreign_call("samba","setval","idmap uid","15000-20000", "");
	&foreign_call("samba","setval","idmap gid","15000-20000", "");
	&foreign_call("samba","setval","winbind uid","15000-20000","");
	&foreign_call("samba","setval","winbind gid","15000-20000","");
	&foreign_call("samba","setval","winbind enum users","yes","");
	&foreign_call("samba","setval","winbind enum groups","yes","");
	&foreign_call("samba","setval","winbind separator","/","");
	&foreign_call("samba","setval","server signing","auto","");
	&foreign_call("samba","setval","null passwords","no","");
	&foreign_call("samba","setval","client use spnego","no","");
	my $out2 = &ez_system("/etc/init.d/smb", "restart");
	sleep(5);
	$success = &ez_system("/usr/bin/net", "ads", "join");
        #$success = ez_system("/usr/bin/net", "ads", "join", "-U", $in{'userin'}."%".$in{'passin'});
	#system($str);	
	if ($success == 0)
	{
		#&foreign_call("samba","setval","security","ADS", "");
#	&foreign_call("samba","setval","workgroup",$in{'domainin'}, "");

		&nas_syslog("servermin", "naslog_info", 33003, "Server join into domain $in{'domainin'} success");
	}
	else
	{
	        &foreign_call("samba","setval","security","user", "");	
	        &foreign_call("samba","delval","idmap gid");
       		&foreign_call("samba","delval","idmap uid");
        	&foreign_call("samba","delval","winbind uid");
        	&foreign_call("samba","delval","winbind gid");
        	&foreign_call("samba","delval","realm");
        	&foreign_call("samba","delval","netbios name");
        	&foreign_call("samba","delval","server signing");
        	&foreign_call("samba","delval","winbind enum users");
        	&foreign_call("samba","delval","winbind enum groups");
        	&foreign_call("samba","delval","null passwords");
        	&foreign_call("samba","delval","client use spnego");
		&foreign_call("samba","delval","winbind separator");
		&nas_syslog("servermin", "naslog_warning", 33004, "Server join into domain $in{'domainin'} failed $success");
	}
}
if ($global)
{
	&foreign_call("samba","modify_share", "global", "global");
}
else
{
	&foreign_call("samba","create_share", "global");
}

&unlock_file($econfig{'smb_conf'});

my $out = &ez_system("/etc/init.d/smb", "restart");

sleep(2);

my $out = &ez_system(split(/\s/, $config{'winbind_store_passwd'}), $in{'userin'}."\%".$in{'passin'});
my $out = &ez_system(split(/\s/, $config{'winbind_restart'}));
sleep(5);

my $out = &ez_system("getent", "passwd");

sleep(1);
		
`$config{'winbind_info_user'} > $config{'pdcuserfile'} 2>/dev/null`;
`$config{'winbind_info_group'} > $config{'pdcgroupfile'} 2>/dev/null`;
		
&foreign_require("pdcusrgroup", "pdcusrgroup-lib.pl");
my $error_msg = &foreign_call("pdcusrgroup","checkpdcfile");

&error($text{'join_domain_fail'}) if ($success != 0);

if ($in{'memberrdn'} =~ /0/)
{
	&redirect("index.cgi");
}
elsif ( $success == 0)
{
	&header($text{'index_title'}, "", undef, 0, 1);
	print &text('join_domain_success', $in{'domainin'});
	&footer("/servermin", $text{'index'});
}
